package com.lzxuni.modules.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.Tree;
import com.lzxuni.modules.common.mapper.FileEntityMapper;
import com.lzxuni.modules.enterprise.entity.EnterpriseInfo;
import com.lzxuni.modules.enterprise.mapper.EnterpriseInfoMapper;
import com.lzxuni.modules.enterprise.service.EnterpriseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  企业信息 服务实现类
 * </p>
 *
 * @author liyl
 * @since 2019-06-27
 */
@Service
public class EnterpriseInfoServiceImpl extends ServiceImpl<EnterpriseInfoMapper, EnterpriseInfo> implements EnterpriseInfoService {

    @Autowired
    private EnterpriseInfoMapper enterpriseInfoMapper;

    @Autowired
    private FileEntityMapper fileEntityMapper;

    //数据查询分页展示、模糊查询
    @Override
    public PageInfo<EnterpriseInfo> queryPage(PageParameter pageParameter, EnterpriseInfo enterpriseInfo) throws SQLException {
        PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                pageParameter.getSidx() + " " + pageParameter.getSord());
        List<EnterpriseInfo> list;
        if (StringUtils.isNotEmpty(enterpriseInfo.getEnterpriseName())) {
            list = baseMapper.selectList(new QueryWrapper<EnterpriseInfo>().like("enterprise_name", enterpriseInfo.getEnterpriseName()));
        }else {
            list = baseMapper.selectList(new QueryWrapper<EnterpriseInfo>());
            for (EnterpriseInfo info : list) {
                System.out.println("倒序 = " + info.toString());
            }
        }
        PageInfo<EnterpriseInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    @Override
    public List<Tree> getTreeSelectTree(String nature) {
        List<Tree> list = new ArrayList<>();
        List<EnterpriseInfo> enterpriseInfos = baseMapper.selectList(new QueryWrapper<>());
        if (enterpriseInfos != null && enterpriseInfos.size() > 0) {
            for (EnterpriseInfo enterpriseInfo : enterpriseInfos) {
                Tree tree = new Tree();
                tree.setId(enterpriseInfo.getYwId());
                tree.setParentId("");
                tree.setText(enterpriseInfo.getEnterpriseName());
                tree.setTitle(enterpriseInfo.getEnterpriseName());
                tree.setShowcheck(false);
                tree.setCheckstate(0);
                tree.setComplete(true);
                list.add(tree);
            }
        }
        return list;
    }

    @Override
    public PageInfo<EnterpriseInfo> queryPagebm(PageParameter pageParameter) {
        PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                pageParameter.getSidx() + " " + pageParameter.getSord());
        List<EnterpriseInfo> list = enterpriseInfoMapper.selectList(new QueryWrapper<EnterpriseInfo>().eq("approval_result","通过"));
        List<FileEntity> filelist = null;
        //FileEntity fileEntity1 = null;
        for (int i = 0;i < list.size();i ++){
            FileEntity fileEntity = new FileEntity();
            /*String defaultUrl = "resources\\static\\images\\l_1.jpg";*/
            String id = list.get(i).getYwId();
            fileEntity.setYwId(id);
            fileEntity.setYwType("demopic");
            filelist = fileEntityMapper.selectList(new QueryWrapper<>(fileEntity));
            //fileEntity1 = fileEntityMapper.selectOne(new QueryWrapper<>(fileEntity));
            if(filelist != null && filelist.size() != 0){
                //fileEntity1 = fileEntityMapper.selectOne(new QueryWrapper<>(fileEntity));
                list.get(i).setUrlsPath(filelist.get(0).getUrlsPath());
            }
            System.out.println("fileEntity1"+filelist);
            System.out.println("listUrlPath" + list.get(i).getUrlsPath());

        }
        PageInfo<EnterpriseInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
